/**
 * Created by fangcailiang on 16/6/18.
 */

/*
 给定一个字符串 判断字符串是不是回文字符串 如 abccba, 为回文，abceba,不是回文zifuc
 */

function isPalindromeStr(str) {

    let strArr = str.split('');
    let len = strArr.length;
    for (let i = 0; i < Math.round(len/2); i++) {
        if ((strArr[i] != strArr[len - 1 - i])) {
            return false;
        }
    }
    return true;
}

console.log(isPalindromeStr("abccba"));
console.log(isPalindromeStr("abc1cba"));
console.log(isPalindromeStr("abceacba"));
console.log(isPalindromeStr('1221'));
console.log(isPalindromeStr('中国国中'));
console.log(isPalindromeStr('中国送到国中'));


function isAlpha(char){
    if((char >= '0' && char <= '9') || (char >= 'a' && char <= 'z')){
        return true;
    }
    return false;
}

// 如果忽略大小写，并且只考虑数字和字母,比如 A man, a plan, a canal: Panama  就是一个回文字符串
function isPalindromeStr2(str) {

    if(null == str || str.length < 1){
        return true;
    }
    let strArr = str.toLowerCase().split('');
    let len = strArr.length;
    for (let i = 0, j = len - 1; i < j; i++, j --) {
        while(i<j && !isAlpha(strArr[i])){
            i ++;
        }
        while(i<j && !isAlpha(strArr[j])){
            j --;
        }
        if(strArr[i] != strArr[j]){
            return false;
        }
    }
    return true;
}

console.log(isPalindromeStr2("abcCba"));
console.log(isPalindromeStr2("abc,1!cba"));
console.log(isPalindromeStr2("abceacba"));
console.log(isPalindromeStr2('1221'));
console.log(isPalindromeStr2('中国国中'));
console.log(isPalindromeStr2('中国送到国中'));
console.log(isPalindromeStr2('A man, a plan, a canal: Panama'));